//package com.mediacloud.app.newsmodule.fragment
//
//import android.content.Context
//import android.content.Intent
//import android.os.Build
//import android.view.LayoutInflater
//import android.view.View
//import android.view.ViewGroup
//import android.widget.FrameLayout
//import android.widget.ImageView
//import android.widget.LinearLayout
//import android.widget.TextView
//import androidx.annotation.RequiresApi
//import androidx.lifecycle.Observer
//import androidx.recyclerview.widget.GridLayoutManager
//import androidx.recyclerview.widget.RecyclerView
//import com.mediacloud.app.model.component.ComponentItem
//import com.mediacloud.app.model.news.ArticleItem
//import com.mediacloud.app.model.news.CatalogItem
//import com.mediacloud.app.model.news.kt.TypeX
//import com.mediacloud.app.model.utils.ViewUtils
//import com.mediacloud.app.newsmodule.R
//
//import com.mediacloud.app.newsmodule.adaptor.comment.x.XCommentActivity
//import com.mediacloud.app.newsmodule.adaptor.component.ComponentListItemStyleAdaptor
//import com.mediacloud.app.newsmodule.fragment.video.vod.PlayClickListener
//import com.mediacloud.app.newsmodule.utils.CollectionController
//import com.mediacloud.app.newsmodule.utils.CollectionController.CollectStatusListener
//import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
//import com.mediacloud.app.newsmodule.utils.ShareGridPopUtils
//import com.mediacloud.app.newsmodule.utils.load
//import com.mediacloud.app.newsmodule.utils.spider.SpiderKit
//import com.mediacloud.app.newsmodule.utils.spider.SpiderStatus
//import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig
//import com.mediacloud.app.util.formatNumber
//import com.zimeiti.details.MediaAuthorDetailActivity
//
//class InfoFlowAdapterB(val context: Context, var catalogItem: CatalogItem?) :
//    RecyclerView.Adapter<RecyclerView.ViewHolder>(),
//    View.OnClickListener {
//    var articleItems: MutableList<Any?> = ArrayList()
//    var reload = false
//    var mPlayClickListener: PlayClickListener? = null
//    var componentAdapter: ComponentListItemStyleAdaptor? = null
//    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
//
//        return when (viewType) {
//            0 -> {
//                SmallViewHolder(
//                    LayoutInflater.from(context)
//                        .inflate(R.layout.item_info_flow_small, parent, false)
//                )
//            }
//            1 -> {
//                BigViewHolder(
//                    LayoutInflater.from(context).inflate(R.layout.item_info_flow_big, parent, false)
//                )
//            }
//            else -> {
//                ComponentViewHolder(
//                    LayoutInflater.from(context)
//                        .inflate(R.layout.item_info_flow_header, parent, false)
//                )
//            }
//        }
//
//    }
//
//    init {
//        componentAdapter = ComponentListItemStyleAdaptor(context, catalogItem)
//    }
//
//    fun setItems(articleItems: MutableList<ArticleItem>) {
//        this.articleItems.clear()
//        this.articleItems.addAll(articleItems)
//        notifyDataSetChanged()
//    }
//
//    fun addItems(articleItems: MutableList<ArticleItem>) {
//        var positionStart = this.articleItems.size - 1
//        if (positionStart < 0) positionStart = 0
//        this.articleItems.addAll(articleItems)
//        notifyItemRangeChanged(positionStart, this.articleItems.size)
//
//    }
//
//    fun addItem(item: ArticleItem) {
//        this.articleItems.add(item)
//        notifyItemChanged(this.articleItems.size - 1)
//    }
//
//    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
//    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
//        val item = articleItems[position]
//
//        when (getItemViewType(position)) {
//            0 -> {
//                item as ArticleItem
//                val holder = viewHolder as SmallViewHolder
//                holder.titleTv.text = item.title
//                holder.logoIv.load(
//                    item.logo,
//                    0,
//                    AppFactoryGlobalConfig.getDefaultImageLoadDrawable()
//                )
//                when (item.type) {
//                    TypeX.NEWS.VIDEO -> {
//                        holder.videoTag.visibility = View.VISIBLE
//                        holder.durationTv.visibility = View.VISIBLE
//                        holder.durationTv.text = item.prop4
//                    }
//                    TypeX.NEWS.LIVE_VIDEO -> {
//                        holder.videoTag.visibility = View.VISIBLE
//                        holder.durationTv.visibility = View.GONE
//                    }
//                    else -> {
//                        holder.videoTag.visibility = View.GONE
//                        holder.durationTv.visibility = View.GONE
//                    }
//                }
//                viewHolder.itemView.tag = position
//                viewHolder.itemView.setOnClickListener(this)
//            }
//            1 -> {
//                val holder = viewHolder as BigViewHolder
//                item as ArticleItem
//                holder.setInfo(item, mPlayClickListener)
//                viewHolder.itemView.tag = position
//                viewHolder.itemView.setOnClickListener(this)
//            }
//            else -> {
//                if (!reload) return
//                reload = false
//                val holder = viewHolder as ComponentViewHolder
//                val components = item as MutableList<ComponentItem>?
//                holder.componentContainer.removeAllViews()
//                components?.let {
//                    componentAdapter?.listContentData = it
//                    componentAdapter?.clearEvent()
//                    for (index in 0 until it.size) {
//                        val view = componentAdapter?.getView(index, null, holder.componentContainer)
//                            ?: continue
//                        holder.componentContainer.addView(view)
//                    }
//                }
//            }
//        }
//    }
//
//    override fun getItemCount(): Int {
//        return articleItems.size
//    }
//
//    override fun getItemViewType(position: Int): Int {
//        val item = articleItems[position]
//        return if (item is ArticleItem) {
//            item.infoFlowBig
//        } else {
//            2
//        }
//    }
//
//    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
//        super.onAttachedToRecyclerView(recyclerView)
//        val manager = recyclerView.layoutManager
//        if (manager is GridLayoutManager) {
//            manager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
//                override fun getSpanSize(position: Int): Int {
//                    //注意：这个getSpanSize()方法返回值并不是要显示的列数。解释：getSpanSize返回值不是表示控制每行有几列，而是当前这个Item占这一行得几列
//                    val type = getItemViewType(position)
//                    return if (type == 0) {
//                        1
//                    } else {
//                        2
//                    }
//                }
//            }
//        }
//    }
//
//    class ComponentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
//        val componentContainer: LinearLayout = itemView.findViewById(R.id.componentContainer)
//    }
//
//    class SmallViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
//        val titleTv: TextView = itemView.findViewById(R.id.titleTv)
//        val logoIv: ImageView = itemView.findViewById(R.id.logoIv)
//        val durationTv: TextView = itemView.findViewById(R.id.durationTv)
//        val videoTag: View = itemView.findViewById(R.id.videoTag)
//    }
//
//    class BigViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
//        Observer<SpiderStatus> {
//        val titleTv: TextView = itemView.findViewById(R.id.titleTv)
//        val logoIv: ImageView = itemView.findViewById(R.id.logoIv)
//        val videoTag: View = itemView.findViewById(R.id.videoTag)
//        val listSpider: View = itemView.findViewById(R.id.listSpider)
//        val spiderAvatar: ImageView = itemView.findViewById(R.id.spiderAvatar)
//        val spiderName: TextView = itemView.findViewById(R.id.spiderName)
//        val spiderAttention: TextView = itemView.findViewById(R.id.spiderAttention)
//        val tv_share_count: TextView = itemView.findViewById(R.id.tv_share_count)
//        var iv_share: View = itemView.findViewById(R.id.iv_share)
//        var iv_comment: View = itemView.findViewById(R.id.iv_comment)
//        var iv_collection: View = itemView.findViewById(R.id.iv_collection)
//        var split_line: View = itemView.findViewById(R.id.split_line)
//        val durationTv: TextView = itemView.findViewById(R.id.durationTv)
//        var item: ArticleItem? = null
//        var collectSourceId: String? = null
//        var logoContainer: FrameLayout = itemView.findViewById(R.id.logoContainer)
//        var collectionController: CollectionController? = null
//
//        init {
//
//            collectionController =
//                CollectionController(itemView.context, null, item, object : CollectStatusListener {
//                    override fun onCollectionStatus(sourceId: String, collected: Boolean) {
//                        if (sourceId == collectSourceId) {
//                            iv_collection.isSelected = collected
//                        }
//
//                    }
//
//                    override fun onCollectionOk(sourceId: String, collected: Boolean, msg: String) {
//                        if (sourceId == collectSourceId) {
//                            iv_collection.isSelected = collected
//                        }
//                    }
//
//                    override fun onCollectionErr(msg: String) {}
//                })
//
//        }
//
//        fun setInfo(item: ArticleItem, mPlayClickListener: PlayClickListener?) {
//            this.item = item
//            titleTv.text = item.title
//            logoIv.load(item.logo, 0, AppFactoryGlobalConfig.getDefaultImageLoadDrawable())
//            when (item.type) {
//                TypeX.NEWS.VIDEO -> {
//                    videoTag.visibility = View.VISIBLE
//                    durationTv.visibility = View.VISIBLE
//                    durationTv.text = item.prop4
//                    videoTag.setOnClickListener {
//                        mPlayClickListener?.onPlayIcoClick(itemView, logoContainer, item)
//                    }
//                }
//                TypeX.NEWS.LIVE_VIDEO -> {
//                    videoTag.visibility = View.VISIBLE
//                    durationTv.visibility = View.GONE
//                    videoTag.setOnClickListener {
//                        mPlayClickListener?.onPlayIcoClick(itemView, logoContainer, item)
//                    }
//                }
//                else -> {
//                    videoTag.visibility = View.GONE
//                    durationTv.visibility = View.GONE
//                }
//            }
//            if (item.getShareCount() > 0) {
//                tv_share_count.text = formatNumber(item.getShareCount());
//                tv_share_count.visibility = View.VISIBLE
//            } else {
//                tv_share_count.visibility = View.GONE
//            }
//            if (item.spider != null) {
//                spiderAttention.isSelected = item.isFollowed
//                SpiderKit.observer(
//                    ViewUtils.searchTintContextHostActivity(itemView.context),
//                    this,
//                    item.spider.userId
//                )
//                spiderAvatar.load(item.spider.userImage, 0, null)
//                spiderName.text = item.spider.userName
//                spiderAttention.setOnClickListener {
//                    SpiderKit.attention(
//                        itemView.context,
//                        item.spider.userId,
//                        null,
//                        spiderAttention
//                    );
//                }
//                spiderAttention.visibility = View.VISIBLE
//                split_line.visibility = View.VISIBLE
//                listSpider.setOnClickListener { v: View? ->
//                    MediaAuthorDetailActivity.start(
//                        listSpider.context,
//                        item.spider.userId
//                    )
//                }
//            } else {
//                spiderAvatar.setImageResource(R.drawable.ic_launcher)
//                item.getAuthor().also { spiderName.text = it }
//                spiderAttention.visibility = View.GONE
//                split_line.visibility = View.GONE
//                listSpider.setOnClickListener(null)
//            }
//            iv_share.setOnClickListener {
//                try {
//                    ShareGridPopUtils.show(
//                        itemView.context,
//                        item,
//                        null,
//                        false,
//                        false,
//                        ViewUtils.searchTintContextHostActivity(itemView.context).window.decorView
//                    );
//                } catch (e: Exception) {
//                    e.printStackTrace();
//                }
//            }
//            iv_comment.setOnClickListener {
//                try {
//                    val intent = Intent()
//                    intent.putExtra("data", item)
//                    intent.setClass(itemView.context, XCommentActivity::class.java)
//                    itemView.context.startActivity(intent)
//                } catch (e: java.lang.Exception) {
//                    e.printStackTrace()
//                }
//            }
//
//            collectionController?.articleItem = item
//            collectSourceId = CollectionController.convertappfacSourceId(item)
//            collectionController?.checkCollection()
//            iv_collection.setOnClickListener {
//                collectionController?.collection()
//            }
//        }
//
//        override fun onChanged(status: SpiderStatus) {
//
//
//            item?.let {
//                if (it.spider_user != null && it.spider_user.userId == status.id) {
//                    spiderAttention.isSelected = status.status
//                    if (spiderAttention.isSelected) {
//                        spiderAttention.text = "已关注"
//                    } else {
//                        spiderAttention.text = "关注"
//                    }
//                }
//
//            }
//
//        }
//    }
//
//    override fun onClick(v: View?) {
//        val position = v?.tag as Int
//        val item = articleItems[position] as ArticleItem
//        NewsItemClickUtils.OpenItemNewsHandle(
//            context,
//            item.type,
//            item,
//            catalogItem ?: CatalogItem()
//        )
//    }
//
//}